<?php
namespace App\Http\Controllers\Store;

use App\Exceptions\ApiException;
use App\Http\Requests\StoreInfoRequest;
use App\Http\Requests\BasicRequest;
use App\Models\Account;
use App\Models\Bank;
use App\Models\ShopOrderSplit;
use App\Models\Withdraw;
use App\Models\AccountLog;
use App\Models\ShopOrder;
use App\Http\Requests\WithdrawRequest;
use Illuminate\Support\Facades\DB;

class AccountController extends CommonController {

    //账户明细
    public function index(BasicRequest $request){

        $account_direction = $request->input('account_direction');
        $amount = Account::where(['uid'=>$this->uid])->value('amount');
        $account_log = AccountLog::select('amount','account_describe','account_type','amount','account_direction','created_at','account_original')
            ->where(['uid'=>$this->uid,'is_del'=>0])
            ->when($account_direction,function($query) use ($account_direction){
                if($account_direction) $query->where(['account_direction'=>$account_direction]);
            })
            ->pages();
        return json_success('ok',['amount'=>$amount,'account_log'=>$account_log]);

    }

    //提现
    public function withdraw(BasicRequest $request){
        $return = array();
        $return['amount'] = Account::where(['uid'=>$this->uid])->value('amount');
        $bank_info = Bank::select('bank','bank_card')->where(['uid'=>$this->uid,'is_del'=>0])->first();
        $return['bank'] = $bank_info->bank;
        $return['bank_card'] = mb_substr($bank_info->bank_card,-4);

        return json_success('OK',$return);
    }

    //提现申请
    public function withdraw_submit(WithdrawRequest $request){
        $withdraw_amount = floatval($request->input('amount'));
        $withdraw_sn = DB::transaction(function () use($withdraw_amount) {
            $uid = $this->uid;
            $amount =  Account::where(['uid'=>$uid])->value('amount');

            if($withdraw_amount % 100!=0){
                throw new \Exception('提现金额必须为100的整数倍');
            }
            $bank = Bank::select('bank_name','bank_card')->where(['uid'=>$uid,'is_del'=>0])->first();

            if($withdraw_amount>floatval($amount)){
                throw new \Exception('资金不足');
            }
            $withdraw_sn = ShopOrder::createOrderSN();
            //插入资产记录表
            AccountLog::insertAccountLog($uid,$amount,'withdraw',$withdraw_sn);

            //减少资金
            Account::where(['uid'=>$uid])->decrement('amount',$withdraw_amount);
            $withdraw_sn = ShopOrder::createOrderSN();
            //插入提现表
            $withdraw = [
                'uid'=>$uid,
                'withdraw_sn'=> $withdraw_sn,
                'amount'=>$withdraw_amount,
                'status'=>1,
                'bank'=>$bank->bank,
                'bank_name'=>$bank->bank_name,
                'bank_card'=>$bank->bank_card,
            ];
            Withdraw::create($withdraw);

            return $withdraw_sn;
        });
        return json_success('OK',$withdraw_sn);
    }
    //提现记录
    public function withdraw_list(){

        $return['amount'] = Account::where(['uid'=>$this->uid])->value('amount');
        $return['list']=Withdraw::where(['uid'=>$this->uid,'is_del'=>0])->select(['amount','status','withdraw_sn','created_at'])->orderBy('id','desc')->pages();
        return json_success('OK',$return);
    }
    //待结算金额及列表
    public function split_list(BasicRequest $request){
        $status = $request->input('status','1');
        $time = $request->input('time',date('Y-m'));

        $return['amount'] = ShopOrderSplit::where(['store_id'=>$this->store_id])->whereIn('status',[1,2])
            ->where('created_at','>=',date('Y-m').'-01 00:00:00')
            ->sum('store_amount');
        $return['list'] = ShopOrderSplit::where(['store_id'=>$this->store_id,'status'=>$status])
            ->select(DB::raw('CONCAT("订单号:",order_sn," 预计结算时间：",confirm_at) as detail,store_amount as amount,created_at'))
            ->when($time,function($query) use ($time){
                $query->where(function ($query) use($time){
                    $query->where('created_at','>',$time.'-01 00:00:00')->orWhere('confirm_at','>',$time.'-01 00:00:00');
                });
            })
            ->orderBy('created_at','desc')
            ->pages();
        return json_success('OK',$return);
    }
}